﻿using System.Collections.Generic;
using System.Linq;
using CollegeSim.CourseScheduler.Domain.DTO;
using CollegeSim.CourseScheduler.Domain.Interfaces;
using AutoMapper;
using CollegeSim.CourseScheduler.Domain.Interfaces.Repositories;

namespace CollegeSim.CourseScheduler.Persistence
{
    // Author: Ryan Redburn
    // Project: Course Scheduler
    // Date: 2/5/2014

    public class StudentRepository : IStudentRepository
    {
        #region [ Methods ]

        /// <summary>
        /// Returns all Students.
        /// </summary>
        /// <returns>IEnumerable of StudentDto.</returns>
        public IEnumerable<StudentDto> FindAll()
        {
            IEnumerable<StudentDto> students;

            using (var context = new CollegeSimEntities())
            {
                students = Mapper.Map<IEnumerable<StudentDto>>(context.Students);
            }

            return students;
        }

        /// <summary>
        /// Returns all Students with the given name.
        /// </summary>
        /// <param name="firstName">Student first name.</param>
        /// <param name="lastName">Student last name.</param>
        /// <returns>IEnumerable of StudentDto.</returns>
        public IEnumerable<StudentDto> FindByName(string firstName, string lastName)
        {
            IEnumerable<StudentDto> students;

            using (var context = new CollegeSimEntities())
            {
                students = Mapper.Map<IEnumerable<StudentDto>>(context.Students
                    .Where(s => s.FirstName == firstName && s.LastName == lastName));
            }

            return students;
        }

        /// <summary>
        /// Returns a Student with the given id.
        /// </summary>
        /// <param name="studentId">Student integer id.</param>
        /// <returns>IEnumerable of StudentDto.</returns>
        public StudentDto FindById(int studentId)
        {
            StudentDto student;

            using (var context = new CollegeSimEntities())
            {
                student = Mapper.Map<StudentDto>(context.Students.Single(s => s.Id == studentId));
            }

            return student;
        }

        /// <summary>
        /// Saves a new Student.
        /// </summary>
        /// <param name="newStudent">New Student to save.</param>
        public void Insert(StudentDto newStudent)
        {
            using (var context = new CollegeSimEntities())
            {
                context.Students.Add(Mapper.Map<Student>(newStudent));
                context.SaveChanges();
            }
        }

        /// <summary>
        /// Updates an existing Student.
        /// </summary>
        /// <param name="studentUpdate">Student to update.</param>
        public void Update(StudentDto studentUpdate)
        {
            using (var context = new CollegeSimEntities())
            {
                var student = context.Students.Single(s => s.Id == studentUpdate.Id);
                student.FirstName = studentUpdate.FirstName;
                student.LastName = studentUpdate.LastName;
                student.PhoneNumber = studentUpdate.PhoneNumber;
                student.Email = studentUpdate.Email;
                student.YearsAttended = studentUpdate.YearsAttended;
                student.GPA = studentUpdate.GPA;
                context.SaveChanges();
            }
        }

        /// <summary>
        /// Deletes an existing Student.
        /// </summary>
        /// <param name="studentDel">Student to delete.</param>
        public void Delete(StudentDto studentDel)
        {
            using (var context = new CollegeSimEntities())
            {
                context.Students.Remove(context.Students.Single(s => s.Id == studentDel.Id));
                context.SaveChanges();
            }
        }

        #endregion
    }
}
